import axios from 'axios'
export default {
  namespaced: true,
  state: {
    list: []
  },
  mutations: {
    upList(state, newlist) {
      state.list = newlist
    }
  },
  actions: {
    async getList({ commit }) {
      const { data } = await axios.get('http://localhost:3000/todos')
      console.log(data)
      commit('upList', data)
    },
    async addList({ dispatch }, { name, done }) {
      const { data } = await axios({
        url: 'http://localhost:3000/todos',
        method:'post',
        data: { 
          name: name, 
          done: done 
        }
      })
      console.log(data)
      dispatch('getList')
    },
    async upList({ dispatch }, { id, done }) {
      const { data } = await axios({
        url: `http://localhost:3000/todos/${id}`,
        method: 'patch',
        data: {
          done: done
        }
      })
      console.log(data)
      dispatch('getList')
    },
    async delList({ dispatch }, id) {
      const { data } = await axios.delete(`http://localhost:3000/todos/${id}`)
      console.log(data)
      dispatch('getList')
    }
  }
}
